import 'package:cloud_medicine/pages/about.dart';
import 'package:cloud_medicine/pages/address_list.dart';
import 'package:cloud_medicine/pages/alter_password.dart';
import 'package:cloud_medicine/pages/back.dart';
import 'package:cloud_medicine/pages/chat_list.dart';
import 'package:cloud_medicine/pages/error_center.dart';
import 'package:cloud_medicine/pages/loading.dart';
import 'package:cloud_medicine/pages/login_select.dart';
import 'package:cloud_medicine/pages/messgae_list.dart';
import 'package:cloud_medicine/pages/my_collect.dart';
import 'package:cloud_medicine/pages/order_list.dart';
import 'package:cloud_medicine/pages/prescription_list.dart';
import 'package:cloud_medicine/pages/search.dart';
import 'package:cloud_medicine/pages/shop.dart';
import 'package:cloud_medicine/pages/my.dart';
import 'package:cloud_medicine/pages/post.dart';
import 'package:cloud_medicine/pages/shop_manage.dart';
import 'package:cloud_medicine/pages/user.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'pages/login.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        fontFamily: 'main', // 设置全局字体
        primarySwatch: Colors.blue,
      ),
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('zh', 'CN'),
        const Locale('en', 'US'),
      ],
      debugShowCheckedModeBanner: false,
      // 取消debug字条
      initialRoute: '/start',
      // 开始路由
      routes: <String, WidgetBuilder>{
        '/start': (BuildContext context) => LoadingPage(),
        '/home': (BuildContext context) => MyHomePage(),
        '/loginSelect': (BuildContext context) => LoginSelectPage(),
        '/login': (BuildContext context) => LoginPage(),
        '/user': (BuildContext context) => UserPage(),
        '/message': (BuildContext context) => MessageListPage(),
        '/about': (BuildContext context) => AboutPage(),
        '/alter': (BuildContext context) => AlterPasswordPage(),
        '/search': (BuildContext context) => SearchPage(),
        '/collectShop': (BuildContext context) => MyCollect(),
        '/address': (BuildContext context) => AddressListPage(),
        '/back': (BuildContext context) => BackPage(),
        '/orderList': (BuildContext context) => OrderList(),
        '/shopManage': (BuildContext context) => ShopManage(),
        '/error': (BuildContext context) => ErrorCenter()
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  int initIndex;

  MyHomePage({Key key, this.initIndex = 0}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  TabController tabcontroller; //Tab页的控制器，可以用来定义Tab标签和内容页的坐标
  DateTime lastPopTime; //上次点击时间

  @override
  void initState() {
    super.initState();
    tabcontroller = new TabController(
        length: 5, //Tab页的个数
        vsync: this, //动画效果的异步处理，默认格式
        initialIndex: widget.initIndex);
  }

  @override
  void dispose() {
    //释放内存，节省开销
    tabcontroller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          if (lastPopTime == null ||
              DateTime.now().difference(lastPopTime) > Duration(seconds: 1)) {
            //两次点击间隔超过1秒则重新计时
            lastPopTime = DateTime.now();
            Fluttertoast.showToast(
                msg: '再按一次退出',
                backgroundColor: Colors.black,
                textColor: Colors.white);
            return new Future.value(false);
          }
          return new Future.value(true);
        },
        child: Scaffold(
            body: new TabBarView(
              controller: tabcontroller,
              children: <Widget>[
                // 三个主体页面
                Medicine(),
                PostPage(),
                ChatListPage(),
                PreScriptionPage(),
                MyPage(),
              ],
            ),
            bottomNavigationBar: new Material(
              color: Colors.white70,
              elevation: 20,
              child: new TabBar(
                controller: tabcontroller,
                tabs: <Tab>[
                  new Tab(child: _tabItem('药店', Icons.store)),
                  new Tab(child: _tabItem('帖子', Icons.assignment)),
                  new Tab(child: _tabItem('聊天', Icons.chat)),
                  new Tab(child: _tabItem('处方', Icons.note_add)),
                  new Tab(child: _tabItem('我的', Icons.person_outline)),
                ],
                //tab被选中时的颜色，设置之后选中的时候，icon和text都会变色
                labelColor: Colors.lightBlueAccent,
                //tab未被选中时的颜色，设置之后选中的时候，icon和text都会变色
                unselectedLabelColor: Colors.black,
                indicator: BoxDecoration(), // 取消下划线
              ),
            )));
  }

  Widget _tabItem(String text, IconData icon) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Icon(icon),
        Text(text, style: TextStyle(fontSize: 10))
      ],
    );
  }
}
